package com.xiaowu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaowu.mapper.BlindMapper;
import com.xiaowu.pojo.Blind;
import com.xiaowu.pojo.Blindgister;
import com.xiaowu.pojo.PageBean;
import com.xiaowu.pojo.StringToDate;
import com.xiaowu.service.BlindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service
public class BlindServiceImpl implements BlindService {
    @Autowired
    private BlindMapper bm;
    //查询
    @Override
    public List<Blind> list() {
        return bm.list();
    }

    //删除
    @Override
    public void delete(Integer id) {
        bm.delete(id);
    }


    //添加
    @Override
    public void add(Blindgister blind) {
        LocalDateTime registerDate=LocalDateTime.now();

        DateTimeFormatter dfDateTime = DateTimeFormatter.ofPattern("yyyy年MM月dd日 EEE HH:mm:ss", Locale.CHINA);
        String format = registerDate.format(dfDateTime);
        String formats = format.replace("周", "星期");
        blind.setRegisterDate(formats);
        blind.setLoginStatus((byte) 0);
        blind.setChannelName(blind.getUserId());
        blind.setIsNeedHelp("no");
        blind.setOnLineStatus("0");
        blind.setGestures_one_ai_question("重庆南岸区未来三天的天气如何？");
        blind.setGestures_two_ai_question("重庆今天有什么新闻热点吗？");
        blind.setGestures_three_ai_question("给我一个关于去重庆旅游的旅行计划安排");
        blind.setGestures_call_number("19823113218");
        bm.insert(blind);
    }

    //分页查询
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //设置分页信息
        PageHelper.startPage(page, pageSize);
        //查询数据
        List<Blind> list = bm.listByPage();
        Page<Blind> p = (Page<Blind>) list;
        //封装
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    //分页条件查询
    @Override
    public PageBean pages(Integer page, Integer pageSize, String userId, String onLineStatus, LocalDate begin, LocalDate end) throws Exception {
        //设置分页信息
        PageHelper.startPage(page, pageSize);
        //查询数据
        //List<Blind> list = bm.listByPages(userId, onLineStatus);
        Page<Blind> pageResult = (Page<Blind>) bm.listByPages(userId, onLineStatus);
        List<Blind> list = pageResult.getResult();
        if(begin !=null && end !=null){
            List<Blind> list1 =new ArrayList<>();
            StringToDate st=new StringToDate();
            for (Blind blind : list) {
                String registerDate = blind.getRegisterDate();
                    LocalDate localDate = st.StringToDate(registerDate);

                    if(localDate.isAfter(begin)&&localDate.isBefore(end)){
                        list1.add(blind);

                }

            }
           list=list1;
        }
      /*  Page<Blind> p = (Page<Blind>) list;
        //封装
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());*/
        PageBean pageBean = new PageBean(pageResult.getTotal(), list);
        return pageBean;
    }

}
